package com.longevityassistant.activity;

import java.util.HashMap;
import java.util.Map;

import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;
import app.utils.CommonUtils;
import app.utils.Tools;
import app.widget.PAPopupWindow;
import app.widget.ScrollerMenu;

import com.longevityassistant.activity.home.FamilyListActivity;
import com.longevityassistant.menu.CommonMenuUtil;

/**
 * 非登录区，用于嵌套“登录”，“热销”，“产品”，“更多” [功能详细描述]
 * 
 * @author EX-CAILEI001
 * @version [Android PABank C01, 2012-2-29]
 */
public class MainActivity extends MenuActivity {

	private static final String TAG = MainActivity.class.getSimpleName();
	
	/**
	 * 标识 非登录区 “产品” menu 显示的 activity  id ，
	 * "2"表示显示的ProductMainActivity, "FundDetaiMainActivity" 表示 基金详细界面
	 * "FinacingProductDetailActivity"  表示  理财详细界面
	 */
	public static String productActivityKey = "2";
	
	/**
	 * 中间显示内容
	 */
	private LinearLayout llRoot;

	private LinearLayout llMenu;

	private LocalActivityManager localActivityManager;

	private Map<String, Menu> menuMap = new HashMap<String, Menu>();

	@Override
	protected void hideSafeSoftKeyboard() {
		//密码框获得焦点时，弹出输入法，这时点击物理menu键，则隐藏输入法，显示物理menu。

	}

	/**
	 * 菜单改变监听器
	 */
	public OnMenuChangeListener mOnMenuChangeListener;

	private ScrollerMenu scrollerMenu;

	private Context context;

	/**
	 * 当前选中的菜单
	 */
	private int currentMenuIndex = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.layout_main_activity);
		CommonUtils.add(this);
		
		context = this;
		localActivityManager = getLocalActivityManager();

		llRoot = (LinearLayout) findViewById(R.id.ll_root);
		llMenu = (LinearLayout) findViewById(R.id.ll_menu);
		scrollerMenu = (ScrollerMenu) findViewById(R.id.scroll_menu);

		// 添加底部导航条 
		initMenu();

		//导航条改变监听器,在LoginHotActivity 中调用该方法进行导航条滑动
		mOnMenuChangeListener = new OnMenuChangeListener() {
			public void onMenuChange(int whitch) {
				
				currentMenuIndex = whitch;
				
				if(whitch!=0){
					//menu改变只要不是登录menu，基金 或理财产品  购买登录参数 清空
//					LoginActivity.Params.prdType = "";
				}
					
				scrollerMenu.select(whitch, false, llMenu.getChildCount());
			}
		};

		// 初始化 Menu菜单
		initMenu2();
		
		// 默认选中导航条的第一项
		scrollerMenu.select(0, false, llMenu.getChildCount());
		//中间内容显示的activity
		showContentActivity(menuMap.get("0"));
		
		
	}

	/**
	 * 初始化菜单 Menu
	 */
	private void initMenu() {
		Menu menu1 = new Menu();
		menu1.id = "0";
		menu1.drawable = getResources().getDrawable(R.drawable.selector_scrollmenu_item_login);
		menu1.intent = new Intent(this, FamilyListActivity.class);
		addMenu(menu1);
		menuMap.put(menu1.id, menu1);

		Menu menu2 = new Menu();
		menu2.id = "1";
		menu2.drawable = getResources().getDrawable(R.drawable.selector_scrollmenu_item_hot);
		menu2.intent = new Intent(this, AboutActivity.class);
		addMenu(menu2);
		menuMap.put(menu2.id, menu2);

		Menu menu3 = new Menu();
		menu3.id = "2";
		menu3.drawable = getResources().getDrawable(R.drawable.selector_scrollmenu_item_product);
		menu3.intent = new Intent(this, AboutActivity.class);
		addMenu(menu3);
		menuMap.put(menu3.id, menu3);

		Menu menu4 = new Menu();
		menu4.id = "3";
		menu4.drawable = getResources().getDrawable(R.drawable.selector_scrollmenu_item_more);
		menu4.intent = new Intent(this, AboutActivity.class);
		addMenu(menu4);
		menuMap.put(menu4.id, menu4);
		
//		Menu menu5 = new Menu();
//		menu5.id = "4";
//		menu5.drawable = getResources().getDrawable(R.drawable.selector_scrollmenu_item_more);
//		menu5.intent = new Intent(this, AboutActivity.class);
//		addMenu(menu5);
//		menuMap.put(menu5.id, menu5);
//		
//		Menu menu6 = new Menu();
//		menu6.id = "5";
//		menu6.drawable = getResources().getDrawable(R.drawable.selector_scrollmenu_item_more);
//		menu6.intent = new Intent(this, AboutActivity.class);
//		addMenu(menu6);
//		menuMap.put(menu6.id, menu6);
//		
//		Menu menu7 = new Menu();
//		menu7.id = "6";
//		menu7.drawable = getResources().getDrawable(R.drawable.selector_scrollmenu_item_more);
//		menu7.intent = new Intent(this, AboutActivity.class);
//		addMenu(menu7);
//		menuMap.put(menu7.id, menu7);
	}

	/**
	 * [初始化menu菜单]<BR>
	 * [功能详细描述]
	 */
	private void initMenu2() {
		setMenus(CommonMenuUtil.DefaultMenuType.CHECK_UPDATE,
				CommonMenuUtil.DefaultMenuType.ABOUT,
				CommonMenuUtil.DefaultMenuType.EXIT);

	}

	public void showContentActivity(Menu menu) {
		if (menu != null && menu.intent != null) {
			showContentActivity(menu.id, menu.intent);
		}
	}
	
	public void showContentActivity(String id, Intent intent) {
		Window window = localActivityManager.startActivity(String.valueOf(id), intent);
		View view = window != null ? window.getDecorView() : null;

		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT, 1);
		lp.setMargins(0, 0, 0, -Tools.getActualSize(this, 10));
		view.setLayoutParams(lp);
		
		if (llRoot.getChildCount() > 1) {
		    View lastView = llRoot.getChildAt(0);
		    lastView.setVisibility(View.GONE);
			llRoot.removeViewAt(0);
		}
		view.setVisibility(View.VISIBLE);
		llRoot.addView(view, 0);
		
	}

	public void setOnMenuChangeListener(OnMenuChangeListener onMenuChangeListener) {
		mOnMenuChangeListener = onMenuChangeListener;
	}

	public interface OnMenuChangeListener {
		public void onMenuChange(int whitch);
	}

	/**
	 * 添加菜单 Menu
	 * 
	 * @param menu
	 */
	private void addMenu(Menu menu) {
		LinearLayout llRow = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.layout_main_activity_menu_item, null);

		llRow.setLayoutParams(new android.widget.LinearLayout.LayoutParams(
				android.widget.LinearLayout.LayoutParams.FILL_PARENT,
				android.widget.LinearLayout.LayoutParams.WRAP_CONTENT, 1));

		LinearLayout llMenuItem = (LinearLayout) llRow.findViewById(R.id.ll_menu);

		if (menu.drawable != null) {
			llMenuItem.setBackgroundDrawable(menu.drawable);
		}
		if (menu.title != null) {
			TextView tvMenuTitle = (TextView) llRow.findViewById(R.id.tv_menu_title);
			tvMenuTitle.setText(menu.title);
		}

		llMenu.addView(llRow);

		llMenuItem.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				
				//隐藏 基金超市 TableView   PAPopupWindow
				PAPopupWindow.getInstance().dismissPopupWindow();
				
				// 获取选中的菜单index
				currentMenuIndex = getSelectMenuIndex(v);

				// 中间界面显示内容 切换
				changeContent(currentMenuIndex);

				// 菜单 变换 监听
				if (mOnMenuChangeListener != null) {
					mOnMenuChangeListener.onMenuChange(currentMenuIndex);
				}
			}
		});
	}

	/**
	 * 切换 中间显示内容
	 * 
	 * @param menuIndex
	 */
	private void changeContent(int menuIndex) {
		showContentActivity(menuMap.get(""+menuIndex));
	}

	/**
	 * 获取 Menu 选中的下标
	 * 
	 * @param llRow
	 * @return
	 */
	private int getSelectMenuIndex(View llMenuItem) {
		int index = 0;
		if (llMenu != null) {
			for (index = 0; index < llMenu.getChildCount(); index++) {
				if (llMenuItem != null && llMenuItem.getParent().equals(llMenu.getChildAt(index))) {
					return index;
				}
			}
		}

		return index;
	}
	
	class Menu {
		String id;
		Drawable drawable;
		String title;
		Intent intent;
	}
	
	@Override
	protected void onDestroy() {
		CommonUtils.remove(this);
		super.onDestroy();
	}
	
	@Override  
	public boolean onKeyDown(int keyCode, KeyEvent event) 
	{ 
		if(keyCode == KeyEvent.KEYCODE_BACK)
		{ 
			// 退出提示 对话框
			CommonUtils.showTipDialog(MainActivity.this, getString(R.string.msg_exit));
				
			return true;
		}
		
        return super.onKeyDown(keyCode, event);
	} 
}